package com.dstz.crm.core.manager.impl;


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dstz.base.api.query.QueryFilter;
import com.dstz.base.core.util.StringUtil;
import com.dstz.base.db.datasource.DbContextHolder;
import com.dstz.base.manager.impl.BaseManager;
import com.dstz.crm.core.dao.NoticeDao;
import com.dstz.crm.core.manager.NoticeManager;
import com.dstz.crm.core.model.Notice;
import com.dstz.sys.api.constant.RightsObjectConstants;
import com.dstz.sys.api.service.SysAuthorizationService;
import com.dstz.sys.util.ContextUtil;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 案例 Manager处理实现类
 * @author aschs
 * @email aschs@qq.com
 * @time 2018-08-24 18:06:04
 */
@Service("noticeManager")
public class NoticeManagerImpl extends BaseManager<String, Notice> implements NoticeManager{
	@Resource
	NoticeDao noticeDao;
	@Resource
    SysAuthorizationService sysAuthorizationService;

	@Override
	public void saveNotice(Notice notice) {
		String currentUserId = ContextUtil.getCurrentUserId();
		if(StringUtil.isEmpty(notice.getId())) {
			notice.setCreateBy(currentUserId);
			notice.setCreateTime(new Date());
			notice.setReleaseUserId(currentUserId);
			notice.setIsTop(0l);
			this.create(notice);
		}else {
			this.update(notice);
		}
	}


	@Override
	public List<Notice> getByUserId(QueryFilter queryFilter) {
		String userId = ContextUtil.getCurrentUserId();
		 Map<String, Object> userPermission = sysAuthorizationService.getUserRightsSql(RightsObjectConstants.NOTICE, userId, "p.id_");
		 queryFilter.addParams(userPermission);
//	     userPermission.put("userId", userId);
//	     userPermission.put("dbType", DbContextHolder.getDbType());
	     List<Notice> noticeList = noticeDao.getByUser(queryFilter);
	     return noticeList;
	}
	
	@Override
	public void stick(String id, Boolean isTop) {
		Notice notice=new Notice();
		notice.setId(id);
		if(isTop) {
			notice.setIsTop(System.currentTimeMillis());
		}else {
			notice.setIsTop(0l);
		}
		noticeDao.stick(notice);
	}
}
